package com.example.blog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blog.mapper.SendHomeWorkMapper;
import com.example.blog.pojo.Result;
import com.example.blog.pojo.SendHomeWork;
import com.example.blog.service.CorrectService;
import com.example.blog.util.HomeWorkUtils.CorrectUtils;
import com.example.blog.util.TokenUtil.decryptToken;
import com.example.blog.util.TokenUtil.spiltUtils;
import com.example.blog.util.UserUtils.UserUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CorrectServiceImpl extends ServiceImpl<SendHomeWorkMapper, SendHomeWork> implements CorrectService {
    @Resource
    private UserUtils userUtil;
    @Resource
    private SendHomeWorkMapper sendHomeWorkMapper;
    @Resource
    private CorrectUtils correctUtils;

    @Override
    public Result<T> correctHomework(SendHomeWork sendHomeWork, int SendHomeworkId, int score, String valuation, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        int identity = userUtil.getIdentity(userName);
        if (identity == 0) {
            String subject = correctUtils.getSubject(SendHomeworkId);
            QueryWrapper<SendHomeWork> queryWrapper = new QueryWrapper<>();
            QueryWrapper<SendHomeWork> wrapper = queryWrapper.eq("homework_id", SendHomeworkId);
            List<SendHomeWork> workList = sendHomeWorkMapper.selectList(wrapper);
            String json = JSONUtil.toJsonStr(workList);
            SendHomeWork s = JSONObject.parseObject(json, SendHomeWork.class);
            s.setScore(score);
            s.setValuation(valuation);
            s.setSubject(subject);
            int insert = sendHomeWorkMapper.insert(s);
            if (insert > 0) {
                return Result.success("打分成功！");
            }
            return Result.failed();

        }
        return Result.failed();
    }

    @Override
    public Result<List<SendHomeWork>> getUserScore(int SendHomeworkId, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        Integer userId = userUtil.getUserId(userName);
        List<SendHomeWork> rankScore = sendHomeWorkMapper.getRankScore(userId, SendHomeworkId);
        return Result.success(rankScore);
    }

    @Override
    public List<SendHomeWork> getScoreRank(String subject, Date createTime, String token) {
        String pe = decryptToken.decrypt(token);
        String userName = spiltUtils.getUserName(pe);
        int identity = userUtil.getIdentity(userName);
        if (identity==0){
            QueryWrapper<SendHomeWork> queryWrapper = new QueryWrapper<>();
            QueryWrapper<SendHomeWork> Rank = queryWrapper.eq("subject", subject)
                    .eq("create_time",createTime)
                    .orderByDesc("score");
            if (Rank==null){
                return null;
            }
            List<SendHomeWork> RankList = sendHomeWorkMapper.selectList(Rank);
            return RankList;
        }
        return null;
    }

}
